/*
 * Copyright 2009 Douglas Lockamy, dlockamy_at_gmail.com
 * All rights reserved. Distributed under the terms of the MIT License.
 */

std_dow_vfsNode = vfsList[vfsList.length]=new vfsNode();
std_dow_vfsNode.Name = 'stdfs';

function stdNode() {
    fileNode.Name = "";
    fileNode.meta = 0;
    fileNode.next = 0;
    fileNode.prev = 0;
    fileNode.Data = 0;
    fileNode.Parent = 0;
};

var std_fs;

if (!this.stdfs) stdfs = {};

std_dow_vfsNode.item=stdfs;

stdfs.version = '0.1';

stdfs.update = function(event)
{
    alert('!');
    
};

stdfs.init = function()
{
    std_fs = new stdNode();
    std_fs.Name="/";
    std_fs.Type=1;
    std_fs.prev=0;
    std_fs.next=0;
    
    std_fs.Data = new Array();
    std_fs.Data[0] = new stdNode();
    
    var ctl = std_fs.Data[0];
    ctl.Name="ctl";
    ctl.Type=2;
    ctl.prev=0;
    ctl.next=0;
    ctl.Data="a bunch of text";
};

stdfs.mount =  function(serverAddress,fs_type,mountPoint,user,pass,argv)
{
    alert('dont mount to me yet');
};
    
stdfs.umount = function()
{
    std_fs = 0;
    return 0;
};

stdfs.read = function(path,flags,buffer)
{
    if(flags =='1')
    {
        var readNode = stdfs.walk(path,path);

        if(readNode.Type == 1)
            return 1;
        if(readNode.Type==5)
        {
            readNode.Data[3].read(path,flags);
            return -1;
        }           
        else
            return 0;    
    }
    
    if(flags == '2')
    {
       var readNode = stdfs.walk(path,path);

        if(readNode.Type == 1)
            return readNode;
        if(readNode.Type==5)
        {
            return(readNode.Data[3].read(readNode.Name,flags) );
        }           
        else
            return readNode;       
    }
    if(flags =='3')
    {  
        var readNode = stdfs.walk(path,path);
alert(readNode.Name + readNode.Data);
        if(readNode.Type == 1)
        {
            if(readNode.Data[0].Name)
            return readNode.Data[0].Name;
            else
            return "File Read Error"
        }
        if(readNode.Type==5)
        {
            alert('recursive mounts not support yet');
            return(0);//readNode.Data[3].read(readNode.Name,flags) );
        }           
        else
            return readNode.Data;    
    }
    else 
        return -1;
};

stdfs.write = function(path,flags,buffer)
{
/*
    FS File Read Flags
    1 Request Type
    2 Return Array of File in Directory
    3 Read text/data file*/
    var workPath = '/';
    var aNode = 0;
    var pathArray = path.split('/');
    var counter=0;
    
    while(counter <= pathArray.length-2)
    {
        if(workPath == '/')
            workPath = workPath + pathArray[counter];
        else
            workPath = workPath + '/' + pathArray[counter];
        counter++;
    }

    if(flags==0)
    {
        //unlink a node
            counter = 0;
            aNode = stdfs.walk(path,path);
            var prevNode = aNode.prev;   
            var nextNode = aNode.next;      

            while(aNode.Parent.Data[counter].Name != aNode.Name && counter < aNode.Parent.Data.length)
            {
                counter++;
            }
            
            aNode.Parent.Data.splice(counter,1);
            
            prevNode.next = nextNode;
            nextNode.prev = prevNode;
    }
    
    if(flags==1)
    {
        aNode = stdfs.walk(workPath,workPath);

        if(aNode.Type == 1 && buffer == 1)
        {
            aNode.Data[aNode.Data.length] = new fileNode;
            var newDir = aNode.Data[aNode.Data.length-1];
            newDir.Name = pathArray[pathArray.length-1];
            newDir.Type = 1;
            newDir.next = 0;
            newDir.Data = new Array();
            newDir.prev = aNode.Data[aNode.Data.length-1];
            newDir.Parent = aNode;

            if(aNode.Data.length >= 2 )
            {        
                aNode.Data[aNode.Data.length-2].next=newDir;
            }          
        }
        if(aNode.Type == 1 && buffer == 2)
        {
            var newfile = aNode.Data[aNode.Data.length] = new fileNode;
            newfile.Name = pathArray[pathArray.length-1];
            newfile.Type = 2;
            newfile.next = 0;
            newfile.Data = buffer;
            newfile.prev = aNode.Data[aNode.Data.length-1];
            aNode.Data[aNode.Data.length-1].next=newFile;
        }
        
        if(aNode.Type==5 )
        {
            var loadPath = stdfs.BuildMountPath(aNode,path);

            return(aNode.Data[3].write(loadPath,flags,buffer) );
        }  
        
        else
            return -1;
    }
    else if(flags==2)
    {
        return 0;
    }
    else if(flags==3)
    {
    
    }
    else 
        return -1;
};

stdfs.walk = function(searchPath, fsNode)
{
    xNode = 0;
    var nodeArray = fsNode.split("/");
    aNode = std_fs.Data[0];
      
    if(fsNode.length==1 && fsNode.charAt(0) == '/')
    {
        //benix.debug('walk: root path request'); 
        return std_fs;
    }

    if(fsNode.charAt(0) != '/')
    {
        return searchPath + '<br> Not a root based path<br>';
    }

    depth = 1;
    
    while(aNode.Name != nodeArray[nodeArray.length-1])
    {
        while(aNode.Name != nodeArray[depth] && depth <= nodeArray.length )
        {
                if(aNode.next != 0)
                {
                    aNode = aNode.next;
                }
                else
                {
                    return 'File not found<br>';
                }
                                                                                                                                                           
                xNode++;
        };
        
        if(aNode.Name != nodeArray[nodeArray.length-1])
        {
            aNode=aNode.Data[0];
            depth++;
        }
        
        if(aNode.Type == 5)
        {
            return aNode;
        };       
    };
            return aNode;
};


